#pragma once

#include <cstdlib>
#include <unistd.h>
#include <string>

using std::string;

bool GetQueryString(string &query_string)
{
    string method = getenv("METHOD");
    if (method == "GET")
    {
        query_string = getenv("QUERY_STRING");
        return 1;
    }
    else if (method == "POST")
    {
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char ch;
        while (content_length--)
        {
            read(0, &ch, 1);
            query_string.push_back(ch);
        }
        return 1;
    }
    else
    {
        return 0;
    }
    return 0;
}

void CutString(string in, string sep, string &out1, string &out2)
{
    auto pos = in.find(sep);
    if (pos != string::npos)
    {
        out1 = in.substr(0, pos);
        out2 = in.substr(pos + sep.size());
    }
}